var result;
var tab2="  ";
var tab4="    ";
var sid="";
var usr = "";
var curl=window.location.protocol+'//'+window.location.host;
var seqID = 0;

function isArray(obj) {   
	return Object.prototype.toString.call(obj) === '[object Array]'; 
} 

function getSysCurTime(){
	var year, month, day, hour, minute, second;
	var today;
	
	today = new Date();
	year = today.getYear();
	month = today.getMonth();
	day = today.getDate();
	hour = today.getHours();
	minute = today.getMinutes();
	second = today.getSeconds();

	year += 1900;
	month += 1;
	
	return year+"-"+month+"-"+day+"/"+hour+":"+minute+":"+second+"\n";
}

function appendTxtNode(msg, type) {
	var container;
	var separator = "***********************************\n";

	if (type==1)
	{
		container = $(parent.frames[2].document).find('#req').text();
		result = "Request:\n";
		result += getSysCurTime();

		result += separator;
		result += JSON.stringify( msg, null, 4 );
		
		result += "\n";
		result += separator;
		result += container;
		$(parent.frames[2].document).find('#req').text(result);
	}
	else if(type==2)
	{
		result = "Response:\n";
		result += getSysCurTime();

		result += separator;
		result += JSON.stringify( msg, null, 4 );

		container = $(parent.frames[2].document).find('#resp').text();
		result += "\n";
		result += separator;
		result += "\n";
		result += container;
		$(parent.frames[2].document).find('#resp').text(result);
	}
	else{
		alert("Can't find the element to display!");	
	}
}

function onSend()
{
	var dcontent;
	var list=document.getElementById('cmdlist');
	var curIndex=list.options.selectedIndex;
	var value = list.options[curIndex].value;	

	if($('#msg').val())
	{
		var param = $('#msg').val();
		var request = JSON.parse(param);
		if(sid != "" && value!=0)
		{
			request.sid = sid;
		}
		else if (value == 0)
		{
			usr = request.user;
		}
		
		var cmd_list = document.getElementById("cmdlist");
		
		var jsonrpc = {};
		jsonrpc.jsonrpc = "2.0";
		jsonrpc.method = cmd_list.options[cmd_list.selectedIndex].text;
		jsonrpc.params = request;
		jsonrpc.id = ++seqID;
		
		request = jsonrpc;
	}
	else{
		alert("message parameter can't be blank!");
		return;
	}	

	appendTxtNode(request, 1);
	
	dcontent = JSON.stringify(request);
	//alert("data:"+ dcontent);
	
	$.ajax({
			type: "POST",
			url: curl + "/1.cgi",
			datatype: "json",
			data: dcontent,
			success: function(data){
				appendTxtNode(data, 2);
				//alert("resp: " + JSON.stringify(data));
				if(value==0&&data.result != undefined){ // Login
					sid = data.result.sid;
					//alert("sid = "+ sid);
					startWS(); 
				}
				else if(value==1&&data.result != undefined){ // Logout
					stopWS();
					sid = "";
					usr = "";
					ws.close();
				}
			},
			error: function(jqXHR, textStatus, errorThrown){
				alert(textStatus);
			}
	});
}

function onSelChange(sel){
	var value = sel.options[sel.selectedIndex].value;
	var tmp = JSON.parse(cmdParamList[value]);
	
	$('#msg').val(JSON.stringify(tmp, null, 4));
}

function onClear(){
	$(parent.frames[2].document).find('#req').text("");
	$(parent.frames[2].document).find('#resp').text("");
}

function traverse(obj) {
	var type = typeof obj;
		if (type == "object") {
		for (var key in obj) {
			if( obj[key] == null ){
				result += "\n";
				result += tab2;
				result += key;
				result += ":";
				result += "null";					
				continue;
			}
			if(isArray(obj[key])){
				result += "\n";
				result += tab2;
				result += key;
				result += ":\n";
				result += tab2;
				result += "[";
				for(x in obj[key]){
					var ty = typeof obj[key][x];
					if(ty == "object"){
						result += "\n";
						result += tab2;
						result += "{";
						traverse(obj[key][x]);
						result += "\n";	
						result += tab2;
						result += "}";
					}
					else{
						result += "\n";
								result += tab4;
									result += obj[key][x];
					}
				}	
				result += "\n";
				result += tab2;
				result += "]";
			}
			else{
				var typ = typeof obj[key];
				if(typ == "object"){
					result += "\n";
					result += tab2;
					result += key;
					result += ":";
					result += "\n";
					result += tab2;	
					result += "{";
					traverse(obj[key]);
					result += "\n";
					result += tab2;	
					result += "}";
				}
				else{
					result += "\n";
					result += tab2;
					result += key;
					result += ":";
					result += obj[key];	
				}
			}
		}
		}
	else{
		for(var key in obj){
				result += "\n  "+key+":"+obj[key];
		} 	
	}
}
